package test1.de;


import java.security.Key;

import org.apache.shiro.codec.Hex;
import org.apache.shiro.crypto.AesCipherService;
import org.apache.shiro.crypto.BlowfishCipherService;
import org.apache.shiro.crypto.DefaultBlockCipherService;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.DefaultHashService;
import org.apache.shiro.crypto.hash.HashRequest;
import org.apache.shiro.util.ByteSource;
import org.apache.shiro.util.SimpleByteSource;

public class De {

	public static void main(String[] args){
		/*String str= "hello";
		String salt= "123";
		String md5= new Md5Hash(str,salt).toString();
		String md51= new Md5Hash(str,salt,2).toString();
		String md52= new Md5Hash(str,salt,2).toBase64();
		String md53= new Md5Hash(str,salt,2).toHex();
		String sha256= new Sha256Hash(str,salt).toString();
		String sha512= new Sha512Hash(str,salt).toString();
		String sha1 = new Sha1Hash(str,salt).toString();
		String simple= new SimpleHash("MD5",str,salt).toString();
		System.out.println(md5);
		System.out.println(md51);
		System.out.println(md52);
		
		System.out.println(md53);
		System.out.println(sha256);
		System.out.println(sha512);
		System.out.println(sha1);
		System.out.println(simple);*/
		//getDefultDe();
		//createRandomNumber("123");
		//System.out.println(getDefultDe());
		//int a= 1;
		//int b= 2;
		//System.out.println(Math.max(a, b));
		//System.out.println(Math.E);
		//System.out.println(Math.E*Math.E);
		//System.out.println(Math.toRadians(90.0));
		//System.out.println(Math.exp(2.0));
		//System.out.println(Math.floor(1.2));
		//System.out.println(Math.ceil(1.2));
		//System.out.println(Math.round(1.2));
		//System.out.println(Math.IEEEremainder(5,7));
		//System.out.println(Math.abs(-4.5));
		//System.out.println(Math.min(2, 3));
		//System.out.println(Math.nextAfter(1.0, 2.0));
		//System.out.println(Math.nextUp(1.2));
		//System.out.println(Math.random());	
		//System.out.println(new Random().nextInt(11));
		//createRandomNumber();
		getAES();
		System.out.println("----------------------");
		//getBlowfishSerivce();
		defaltCipherService();
	}
	
	public static String getDefultDe(){
		DefaultHashService default1= new DefaultHashService();
		default1.setHashAlgorithmName("SHA-1");
		default1.setPrivateSalt(new SimpleByteSource("1234"));
		default1.setGeneratePublicSalt(true);
		default1.setRandomNumberGenerator(new SecureRandomNumberGenerator());
		default1.setHashIterations(1);
		
		HashRequest request= new HashRequest.Builder()
				.setAlgorithmName("MD5")
				.setSource(ByteSource.Util.bytes("hello"))
				.setSalt(ByteSource.Util.bytes("123"))
				.setIterations(1)
				.build();
		String md5= default1.computeHash(request).toString();
		//System.out.println(md5);
		//System.out.println(new String(default1.computeHash(request).getSalt().getBytes()));
		//System.out.println(default1.computeHash(request).getBytes().toString());
		//System.out.println(new String(default1.computeHash(request).toString()));
		//System.out.println(new String(default1.getPrivateSalt().getBytes()));
		return md5;
		
	}
	
	public static void getAES(){
		AesCipherService aesCipherService= new AesCipherService();
		aesCipherService.setKeySize(128);
		Key key= aesCipherService.generateNewKey();
		String text= "hello";
		System.out.println(aesCipherService.getModeName());
		System.out.println(aesCipherService.getPaddingSchemeName());
		System.out.println("key的一些配置");
		System.out.println(key.getAlgorithm());
		System.out.println(key.getFormat());
		System.out.println(key.getEncoded().length);
		String encrypt= aesCipherService.encrypt(text.getBytes(), key.getEncoded()).toHex();
		String decrypt= new String(aesCipherService.decrypt(Hex.decode(encrypt), key.getEncoded()).getBytes());
		System.out.println(encrypt);
		System.out.println(decrypt);
	}
	
	public static void createRandomNumber(String seed){
		SecureRandomNumberGenerator secureRandomNumberGenerator= new SecureRandomNumberGenerator();
		secureRandomNumberGenerator.setSeed(seed.getBytes());
		String hex=secureRandomNumberGenerator.nextBytes().toHex();
		System.out.println(hex);
	}
	
	public static void createRandomNumber(){
		SecureRandomNumberGenerator secureRandNumberGenrator= new SecureRandomNumberGenerator();
		secureRandNumberGenrator.setSeed("123".getBytes());
		//secureRandNumberGenrator.setDefaultNextBytesSize(32);
		System.out.println(secureRandNumberGenrator.getDefaultNextBytesSize());
		System.out.println(secureRandNumberGenrator.nextBytes().toHex());
		System.out.println(secureRandNumberGenrator.nextBytes(128).toHex());
	}
	
	public static void getBlowfishSerivce(){
		BlowfishCipherService blow= new BlowfishCipherService();
		blow.setKeySize(128);
		Key key= blow.generateNewKey();
		String text= "hello";
		System.out.println(blow.getModeName());
		System.out.println(blow.getPaddingSchemeName());
		System.out.println("key的一些配置");
		System.out.println(key.getAlgorithm());
		System.out.println(key.getFormat());
		System.out.println(key.getEncoded().length);
		String encrypt= blow.encrypt(text.getBytes(), key.getEncoded()).toHex();
		
		String decrypt= new String(blow.decrypt(Hex.decode(encrypt), key.getEncoded()).getBytes());
		System.out.println(encrypt);
		System.out.println(decrypt);
	}
	
	public static void defaltCipherService(){
		DefaultBlockCipherService defaultBlockCipherService= new DefaultBlockCipherService("AES");
		defaultBlockCipherService.setKeySize(128);
		Key key= defaultBlockCipherService.generateNewKey();
		String text= "hello";
		System.out.println(defaultBlockCipherService.getModeName());
		System.out.println(defaultBlockCipherService.getPaddingSchemeName());
		System.out.println("key的一些配置");
		System.out.println(key.getAlgorithm());
		System.out.println(key.getFormat());
		System.out.println(key.getEncoded().length);
		String encrypt= defaultBlockCipherService.encrypt(text.getBytes(), key.getEncoded()).toHex();
		String decrypt= new String(defaultBlockCipherService.decrypt(Hex.decode(encrypt), key.getEncoded()).getBytes());
		System.out.println(encrypt);
		System.out.println(decrypt);
	}
	
}
